package co.chuangdian.oflauncher.widgets.system;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class IpUtils {


	/**
	 * 获取上网IP：
	 */

	/*public static String getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			ex.printStackTrace();
		}
		return null;
	}*/
	public static String GetNetIp(String ipaddr){  
        URL infoUrl = null;  
        InputStream inStream = null;  
        try {  
            infoUrl = new URL(ipaddr);  
            URLConnection connection = infoUrl.openConnection();  
            HttpURLConnection httpConnection = (HttpURLConnection)connection;  
            httpConnection.setRequestMethod("GET");
            httpConnection.setConnectTimeout(8000);
            httpConnection.setReadTimeout(8000);
            int responseCode = httpConnection.getResponseCode();  
           
            if(responseCode == HttpURLConnection.HTTP_OK)  
              {      
                 inStream = httpConnection.getInputStream();     
               BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8"));  
               StringBuilder strber = new StringBuilder();  
               String line = null;  
               while ((line = reader.readLine()) != null)   
                   strber.append(line + "\n");  
               inStream.close();  
               return strber.toString();               
             }  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
       return "";  
    }

}
